package com.admin.education.dataTransferObject.question.Mapper;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.dataTransferObject.answer.Mapper.AnswerMapper;
import com.admin.education.dataTransferObject.question.QuestionTransfer;
import com.admin.education.dataTransferObject.question.Request.CreateAnswerRequest;
import com.admin.education.dataTransferObject.question.Request.CreateQuestionRequest;
import com.admin.education.dataTransferObject.question.Request.UpdateAnswerRequest;
import com.admin.education.dataTransferObject.question.Request.UpdateQuestionRequest;
import com.admin.education.dataTransferObject.subject.Mapper.SubjectMapper;
import com.admin.education.models.Answers;
import com.admin.education.models.Questions;
import com.admin.education.models.TemplateQuestions;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper
public abstract class QuestionMapper {
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Mappings({
            @Mapping(target = "answers", ignore = true),
            @Mapping(target = "subjects", ignore = true)
    })
    public abstract QuestionTransfer mapFrom(Questions questions);

    @Mappings({
            @Mapping(target = "answers", ignore = true)
    })
    public abstract QuestionTransfer mapFrom(TemplateQuestions questions);

    @Mappings({})
    public abstract List<QuestionTransfer> mapFrom(List<Questions> questions);

    @Mappings({})
    public abstract List<QuestionTransfer> mapFromTemplate(List<TemplateQuestions> questions);

    @Mappings({
            @Mapping(target = "subjects", ignore = true)
    })
    public abstract Questions mapFrom(CreateQuestionRequest createQuestionRequest) throws BadRequestException;
    @Mappings({})
    public abstract Questions mapFrom(UpdateQuestionRequest updateQuestionRequest,@MappingTarget Questions questions) throws BadRequestException;
    @Mappings({})
    public abstract Answers mapFrom(CreateAnswerRequest createAnswerRequest);

    @Mappings({})
    public abstract List<Answers> mapFromUpdateAnswerRequest(List<UpdateAnswerRequest> updateAnswerRequests);
    @Mappings({})
    public abstract List<Answers> mapFromCreateAnswerRequest(List<CreateAnswerRequest> createAnswerRequests);

    @AfterMapping
    public void afterMapping(Questions questions, @MappingTarget QuestionTransfer questionTransfer) {
        if (null != questions.getAnsweres()) {
            questionTransfer.setAnswers(answerMapper.mapFrom(questions.getAnsweres()));
        }

        if(null!=questions.getSubjects()){
            questionTransfer.setSubjects(subjectMapper.mapFrom(questions.getSubjects()));
        }
    }

    @AfterMapping
    public void afterMapping(TemplateQuestions questions, @MappingTarget QuestionTransfer questionTransfer) {
        if (null != questions.getTemplateAnswers()) {
            questionTransfer.setAnswers(answerMapper.mapFromTemplate(questions.getTemplateAnswers()));
        }
    }

    @AfterMapping
    public void afterMapping(CreateQuestionRequest createQuestionRequest, @MappingTarget Questions questions) throws BadRequestException {


            switch (createQuestionRequest.getQuestionType()){
                case ShortAnswerQuestion:
                    return;

                case MultipleChoiceQuestion:
                    if(null==createQuestionRequest.getAnswers())
                        throw new BadRequestException("至少需要一个答案");

                    if(!createQuestionRequest.getAnswers().stream().anyMatch(p->p.isRightAnswer()))
                        throw new BadRequestException("至少有一个正确答案");
                    break;
                case SingleChoiceQuestion:
                    if(null==createQuestionRequest.getAnswers())
                        throw new BadRequestException("至少需要一个答案");

                    if(createQuestionRequest.getAnswers().stream().filter(p->p.isRightAnswer()).count()!=1)
                        throw new BadRequestException("单选题有且只需一个答案");

                    break;
                case JudgmentQuestion:
                    if(null==createQuestionRequest.getAnswers())
                        throw new BadRequestException("至少需要一个答案");

                    if(createQuestionRequest.getAnswers().stream().filter(p->p.isRightAnswer()).count()!=1)
                        throw new BadRequestException("判断题有且只需一个答案");
                    break;

         }
            List<Answers> answers =this.mapFromCreateAnswerRequest(createQuestionRequest.getAnswers());

            for (Answers answer : answers) {
                answer.setQuestions(questions);
            }

            questions.setAnsweres(answers);
    }
    @AfterMapping
    public void afterMapping(UpdateQuestionRequest updateQuestionRequest, @MappingTarget Questions questions) throws BadRequestException {
            switch (updateQuestionRequest.getQuestionType()){
                case ShortAnswerQuestion:
                    return;

                case MultipleChoiceQuestion:
                    if(null==updateQuestionRequest.getAnswers())
                        throw new BadRequestException("至少需要一个答案");

                    if(!updateQuestionRequest.getAnswers().stream().anyMatch(p->p.isRightAnswer()))
                        throw new BadRequestException("至少有一个正确答案");
                    break;

                case SingleChoiceQuestion:
                    if(null==updateQuestionRequest.getAnswers())
                        throw new BadRequestException("至少需要一个答案");

                    if(updateQuestionRequest.getAnswers().stream().filter(p->p.isRightAnswer()).count()!=1)
                        throw new BadRequestException("单选题有且只需一个答案");
                    break;

                case JudgmentQuestion:
                    if(null==updateQuestionRequest.getAnswers())
                        throw new BadRequestException("至少需要一个答案");

                    if(updateQuestionRequest.getAnswers().stream().filter(p->p.isRightAnswer()).count()!=1)
                        throw new BadRequestException("判断题有且只需一个答案");
                    break;

            }
            List<Answers> answers =this.mapFromUpdateAnswerRequest(updateQuestionRequest.getAnswers());

            for (Answers answer : answers) {
                answer.setQuestions(questions);
            }

            questions.setAnsweres(answers);
    }
}
